package com.tedu.bbs.controller;

import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class ArticleController {
    @RequestMapping("/writeArticle")
    public void write(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理文章上传");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        try(
                Connection connection = DBUtil.getConnection();
        ){
            String sql = "SELECT id FROM userinfo WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,author);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                int id = rs.getInt(1);
                String sql1 = "INSERT INTO article(title,content,u_id) VALUES(?,?,?)";
                PreparedStatement ps1 = connection.prepareStatement(sql1);
                ps1.setString(1,title);
                ps1.setString(2,content);
                ps1.setInt(3,id);
                int num = ps1.executeUpdate();
                if (num>0){
                    response.sendRedirect("/writeArticle_success.html");
                }else {
                    response.sendRedirect("/writeArticle_fail.html");
                }
            }else {
                response.sendRedirect("/have_not_user.html");
            }
        }catch (SQLException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理文章列表");
        String userId = request.getParameter("userId");
        String username = request.getParameter("username");
        System.out.println("该用户的id为:"+userId);
        System.out.println("该用户的用户名为:"+username);

        try(
            Connection connection = DBUtil.getConnection();
        ){
            String sql = "SELECT id,title FROM article WHERE u_id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(userId));
            ResultSet rs = ps.executeQuery();

            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();

            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("    <center>");
            pw.println("        <h1>"+username+"的文章</h1>");
            pw.println("        <table border=\"1\">");
            pw.println("            <tr>");
            pw.println("                <td>文章ID</td>");
            pw.println("                <td>文章标题</td>");
            pw.println("                <td>作者</td>");
            pw.println("            </tr>");
            while (rs.next()){
                pw.println("            <tr>");
                pw.println("                <td>"+rs.getInt("id")+"</td>");
                pw.println("                <td>"+rs.getString("title")+"</td>");
                pw.println("                <td>"+username+"</td>");
                pw.println("            </tr>");
            }
            pw.println("        </table>");
            pw.println("    </center>");
            pw.println("</body>");
            pw.println("</html>");

            rs.close();//结果集释放
            ps.close();//执行计划释放
        }catch (SQLException | IOException e){
            e.printStackTrace();
        }


    }



}
